<?php

namespace  backend\modules\system\controllers;

use Yii;
use common\models\SysUser;
use common\models\SysAgent;
use common\extended\system\Util;
use yii\data\Pagination;

class AgentController extends \backend\controllers\BaseController
{
    public $layout = '/agent';

    /**
     * 代理商列表
     */
    public function actionIndex($login_name = '', $user_name = '', $director = '', $mobile = '')
    {
        $field = ['sys_user.*', 'sys_agent.*'];
        $model = SysUser::find()->select($field)->where(['user_type' => 2, 'status' => [0, 1]])->leftJoin('sys_agent', 'sys_agent.agent_uid = sys_user.uid');
        // 加入搜索条件
        if (!empty($login_name)) { // 账户名
            $model->andWhere(['like' ,'login_name', $login_name]);
        }
        if (!empty($user_name)) { // 代理商
            $model->andWhere(['like', 'user_name', $user_name]);
        }
        if (!empty($director)) { // 负责人
            $model->andWhere(['like', 'director', $director]);
        }
        if (!empty($mobile)) { // 电话
            $model->andWhere(['like', 'mobile', $mobile]);
        }

        $pages = new Pagination(['totalCount' => $model->count(), 'pageSize' => 15]);

        $list = $model->asArray()->orderBy('uid DESC')->limit($pages->limit)->offset($pages->offset)->all();

        return $this->render('index', [
            'list' => $list,
            'page' => $pages
        ]);
    }

    /**
     * 新增代理商
     */
    public function actionAdd()
    {
        $SysUser = new SysUser();
        $SysAgent = new SysAgent();
        $SysUser->setScenario('addAgent');

        $post = Yii::$app->request->post();
        if ($SysUser->load($post) && $SysAgent->load($post)) {
            // 代理商
            $SysUser->user_type = 2;
            $SysUser->user_role = 2;

            if (!$SysUser->checkParamAgent()) {
                $this->error($SysUser->errorMessage);
            }
            if (!$SysAgent->checkParam()) {
                $this->error($SysAgent->errorMessage);
            }

            $Transaction = Yii::$app->db->beginTransaction();

            $SysUser->create_uid = $this->getuserInfo()->uid;
            $SysUser->create_name = $this->getuserInfo()->login_name;
            $SysUser->password = Util::strEncryption($SysUser->password);

            $resultUser = $SysUser->save(false);

            $SysAgent->agent_uid = $SysUser->uid;
            $resultAgent = $SysAgent->save(false);

            if ($resultUser && $resultAgent) {

                $Transaction->commit();
                return $this->redirect('index');
            } else {
                $Transaction->rollBack();
                $this->error('新增失败！');
            }

        } else {
            return $this->render('add', [
                'model' => $SysUser,
                'SysAgent' => $SysAgent
            ]);
        }
    }

    /**
     * 编辑代理商
     * @param int $id
     * @return mixed
     */
    public function actionEdit($id)
    {
        if (!($model = SysUser::findOne($id))) {
            $this->error('数据错误');
        }
        $SysAgent = SysAgent::findOne(['agent_uid' => $id]);
        $model->setScenario('editAgent');

        $post = Yii::$app->request->post();
        if ($model->load($post) && $SysAgent->load($post)) {
            if (!$model->checkParamAgent()) {
                $this->error($model->errorMessage);
            }
            if (!$SysAgent->checkParam()) {
                $this->error($SysAgent->errorMessage);
            }

            if ($_POST['SysUser']['password']) {
                $model->password = $this->strEncryption($_POST['SysUser']['password']);
            }
            if ($model->save(false) && $SysAgent->save(false)) {
                return $this->redirect('index');
            } else {
                $this->error('修改失败');
            }
        } else {
            return $this->render('edit', [
                'model' => $model,
                'SysAgent' => $SysAgent
            ]);
        }
    }

    /**
     * 修改代理商状态代理商
     * @param int $id 代理商id
     */
    public function actionEditstatus($id, $status = 0)
    {
        if (is_numeric($id) && $id > 0 && in_array($status, [-1, 0, 1])) {
            if ((new SysUser())->setStatus($id, $status)) {
                echo '1';
            } else {
                echo '0';
            }
        }
    }

}
